package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.mapper.SpecificationMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;


        @Override
        public List<SpecGroup> querySpecGroupByCid(Long cid) {
            if (cid == null) {
                throw new LyException(ExceptionEnum.GROUP_NOT_FOUND);
            }
            SpecGroup specGroup = new SpecGroup();
            specGroup.setCid(cid);
        List<SpecGroup> list = specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.GROUP_NOT_FOUND);
        }
        return list;
    }

    @Override
    public List<SpecParam> querySpecParams(Long gid, Long cid, Boolean searching, Boolean generic) {
        SpecParam specParam =new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        List<SpecParam> paramList = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(paramList)){
            throw  new LyException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }
        return paramList;
    }

    @Override
    public void saveSpecGroup(SpecGroup specGroup) {
        int insert = specGroupMapper.insert(specGroup);
        if(insert!=1){
            throw  new LyException(ExceptionEnum.GROUP_INSERT_ERROR);
        }

    }

    @Override
    public void deleteSpecGroup(Long id) {
        if(id==null){
            throw  new LyException(ExceptionEnum.INVALID_GROUP);
        }
        SpecGroup specGroup = new SpecGroup();
        specGroup.setId(id);
        int i = specGroupMapper.deleteByPrimaryKey(specGroup);
        if(i!=1){
            throw  new LyException(ExceptionEnum.GROUP_DELETE_ERROR);
        }
    }

    @Override
    public void updateSpecGroup(SpecGroup specGroup) {
        int update = specGroupMapper.updateByPrimaryKey(specGroup);
        if(update!=1){
            throw  new LyException(ExceptionEnum.GROUP_UPDATE_ERROR);
        }
    }

    @Override
    public void saveSpecParam(SpecParam specParam) {
        int count = specParamMapper.insert(specParam);
        if (count != 1) {
            throw new LyException(ExceptionEnum.PARAM_INSERT_ERROR);
        }
    }

    @Override
    public void deleteSpecParam(Long id) {
        if(id==null){
            throw  new LyException(ExceptionEnum.INVALID_PARAM);
        }
        SpecParam   specParam = new SpecParam();
        specParam.setId(id);
        int i = specParamMapper.deleteByPrimaryKey(specParam);
        if(i!=1){
            throw  new LyException(ExceptionEnum.PARAM_DELETE_ERROR);
        }
    }

    @Override
    public void updateSpecParam(SpecParam specParam) {
        int update = specParamMapper.updateByPrimaryKey(specParam);
        if(update!=1){
            throw  new LyException(ExceptionEnum.PARAM_UPDATE_ERROR);
        }
    }

    @Override
    public List<SpecGroup> querySpecsByCid(Long cid) {
        List<SpecGroup> specGroups = querySpecGroupByCid(cid);

        List<SpecParam> specParams = querySpecParams(null, cid, null, null);

        Map<Long, List<SpecParam>> map = new HashMap<>();
        //遍历specParams
        for (SpecParam param : specParams) {
            Long groupId = param.getGroupId();
            if (!map.keySet().contains(param.getGroupId())){
                //map中key不包含这个组ID
                map.put(param.getGroupId(),new ArrayList<>());
            }
            //添加到map中
            map.get(param.getGroupId()).add(param);
        }
        for (SpecGroup specGroup : specGroups) {
            specGroup.setParams(map.get(specGroup.getId()));
        }
        return specGroups;
    }
}
